<?php

	class Url {

		private static $_urlFileUri = 'error_404.php';
		private static $_splittedDomainURI = array();
		private static $_hostName;
		private static $_subDomainValue;
		public static $registerVisit = true;
		public static $self = '/';
		private static $_pageNumber = 1;
		private static $_pageSlug = '';
		public static $pageVars = array();

		public static function processUrl() {
			self::$_urlFileUri = PHP_PATH . 'error_404.php';
			$hostName = str_replace( 'www.', '', $_SERVER[ 'HTTP_HOST' ] );
			$requestUri = explode( '?', $_SERVER[ 'REQUEST_URI' ] );
			$requestUri = trim( $requestUri[ 0 ], '/' );

			$splittedHostName = explode( '.', $hostName );
			$hostLength = count( $splittedHostName );
			if( $hostLength > 2 ) {
				self::$_hostName = $splittedHostName[ $hostLength - 2 ] . '.' . $splittedHostName[ $hostLength - 1 ];
				self::$_subDomainValue = str_replace( '.' . self::$_hostName, '', $hostName );
			} else {
				self::$_hostName = $hostName;
			}

			$varsUri = explode( '/nv/', $requestUri );
			if( count( $varsUri ) > 1 ) {
				self::makePageVars( $varsUri[ 1 ] );
				$requestUri = $varsUri[ 0 ];
			} else {
				self::makePageVars( '' );
			}

			self::$self = $requestUri;
			self::$_splittedDomainURI = $splittedRequestUri = explode( '/', $requestUri );
			while( count( $splittedRequestUri ) > 0 && is_numeric( $splittedRequestUri[ count( $splittedRequestUri ) - 1 ] ) ) {
				self::$_pageNumber = array_pop( $splittedRequestUri );
				$requestUri = implode( '/', $splittedRequestUri );
			}

			if( $requestUri == '' ) {
				self::$_urlFileUri = PHP_PATH . 'home.php';
			} else {
				if( file_exists( PHP_PATH . self::slugToName( $requestUri ) . '.php' ) ) {
					self::$_urlFileUri = PHP_PATH . self::slugToName( $requestUri ) . '.php';
				} else {
					$slug = $requestUri == 'page' ? 'home' : $splittedRequestUri[ 0 ];
					$slugsObj = getMultiple( 'game' );
					if( count( $splittedRequestUri ) == 1 ) {
						$slugsObj = getMultiple( 'game', array( 'slug||' . $splittedRequestUri[ 0 ], 'processed||Y' ) );
					}
					if( $slugsObj->getResultCount() > 0 ) {
						if( file_exists( PHP_PATH . 'game.php' ) ) {
							self::$_pageSlug = $splittedRequestUri[ 0 ];
							self::$_urlFileUri = PHP_PATH . 'game.php';
						}
					} else {
						$fileName = self::slugToName( $slug );
						if( file_exists( PHP_PATH . $fileName . '.php' ) ) {
							self::$_urlFileUri = PHP_PATH . $fileName . '.php';
						}
					}
				}
			}
		}

		private static function slugToName( $slug ) {
			return str_replace( '-', '_', $slug );
		}

		public static function __callStatic( $functionName, $argumentsList ) {
			$varName = '_' . lcfirst( str_replace( 'get', '', $functionName ) );
			if( isset( self::$$varName ) ) {
				return self::$$varName;
			} else {
				throw new Exception( "Call to undefined function $functionName!" );
			}
		}

		private function makePageVars( $varPart ) {
			self::$pageVars[ 'GETPOST' ] = array();
			self::$pageVars[ 'GET' ] = array();
			self::$pageVars[ 'POST' ] = array();
			self::$pageVars[ 'QUERY' ] = array();
			self::$pageVars[ 'COOKIE' ] = $_COOKIE;
			self::$pageVars[ 'FILES' ] = $_FILES;

			if( trim( $varPart ) != '' ) {
				$allVars = explode( '/', $varPart );

				if( ( count( $allVars ) % 2 ) !== 0 ) {
					self::$pageVars[ 'GETPOST' ][ 'ORPHAN' ] = trim( array_pop ( $allVars ) );
				}

				for( $varCount = 0; $varCount < count( $allVars ); $varCount += 2 ) {
					self::$pageVars[ 'GET' ][ trim( $allVars[ $varCount ] ) ] = stripslashes( stripslashes( urldecode( trim( $allVars[ $varCount + 1 ] ) ) ) );
					self::$pageVars[ 'QUERY' ][ trim( $allVars[ $varCount ] ) ] = stripslashes( stripslashes( urldecode( trim( $allVars[ $varCount + 1 ] ) ) ) );
					self::$pageVars[ 'GETPOST' ][ trim( $allVars[ $varCount ] ) ] = stripslashes( stripslashes( urldecode( trim( $allVars[ $varCount + 1 ] ) ) ) );
				}
			}

			foreach( $_GET as $key => $value ) {
				self::$pageVars[ 'GETPOST' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
				self::$pageVars[ 'GET' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
			}

			foreach( $_POST as $key => $value ) {
				self::$pageVars[ 'GETPOST' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
				self::$pageVars[ 'POST' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
			}
		}
	}